package com.tozed.test;

import com.tozed.widget.TitleBar;

import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class KeypadTestActivity extends Base implements OnClickListener
{
	
	private TitleBar mTitleBar;
	private Button mBtnPass, mBtnFail;
	public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);
		setContentView(R.layout.keypad_test);
		
		mTitleBar = (TitleBar)findViewById(R.id.title_bar);
		mTitleBar.setTiteText(R.string.str_test_keypad);
		mTitleBar.hideLeftButton();
		mTitleBar.hideRightButton();
				
		mBtnPass = (Button)findViewById(R.id.btn_pass);
		mBtnFail = (Button)findViewById(R.id.btn_fail);
		mBtnPass.setOnClickListener(this);
		mBtnFail.setOnClickListener(this);
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			Button btn = null;
						
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_0:
				btn = (Button)findViewById(R.id.zero);
				break;
			case KeyEvent.KEYCODE_1:
				btn = (Button)findViewById(R.id.one);
				break;
			case KeyEvent.KEYCODE_2:
				btn = (Button)findViewById(R.id.two);
				break;
			case KeyEvent.KEYCODE_3:
				btn = (Button)findViewById(R.id.three);
				break;
			case KeyEvent.KEYCODE_4:
				btn = (Button)findViewById(R.id.four);
				break;
			case KeyEvent.KEYCODE_5:
				btn = (Button)findViewById(R.id.five);
				break;
			case KeyEvent.KEYCODE_6:
				btn = (Button)findViewById(R.id.six);
				break;
			case KeyEvent.KEYCODE_7:
				btn = (Button)findViewById(R.id.seven);
				break;
			case KeyEvent.KEYCODE_8:
				btn = (Button)findViewById(R.id.eight);
				break;
			case KeyEvent.KEYCODE_9:
				btn = (Button)findViewById(R.id.nine);
				break;
			case KeyEvent.KEYCODE_VOLUME_UP:
				btn = (Button)findViewById(R.id.up);
				break;
			case KeyEvent.KEYCODE_VOLUME_DOWN:
				btn = (Button)findViewById(R.id.down);
				break;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				btn = (Button)findViewById(R.id.left);
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				btn = (Button)findViewById(R.id.right);
				break;
			case KeyEvent.KEYCODE_ENTER:
				btn = (Button)findViewById(R.id.enter);
				break;
			case KeyEvent.KEYCODE_DEL:
				btn = (Button)findViewById(R.id.del);
				break;
			case KeyEvent.KEYCODE_BACK:
				btn = (Button)findViewById(R.id.back);
				break;
			case KeyEvent.KEYCODE_MENU:
				btn = (Button)findViewById(R.id.menu);
				break;
			case KeyEvent.KEYCODE_HOME:
				btn = (Button)findViewById(R.id.home);
				break;
			}
			
			if (btn != null) {
				btn.setBackgroundColor(getResources().getColor(R.color.green));
			}
		}
		else if (event.getAction() == KeyEvent.ACTION_UP){
			Button btn = null;
			
			switch (event.getKeyCode()) {				
				case KeyEvent.KEYCODE_DPAD_LEFT:
					btn = (Button)findViewById(R.id.left);
					break;
				case KeyEvent.KEYCODE_DPAD_RIGHT:
					btn = (Button)findViewById(R.id.right);
					break;			
			}
			
			if (btn != null) {
				btn.setBackgroundColor(getResources().getColor(R.color.green));
			}			
		}		
		return true;
	}
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_pass:
			Intent intent = new Intent();
			intent.putExtra("KEY_TEST_STATUS", 1);
			setResult(RESULT_OK, intent);
			finish();
			break;
			
		case R.id.btn_fail:
			Intent intent2 = new Intent();
			intent2.putExtra("KEY_TEST_STATUS", 0);
			setResult(RESULT_OK, intent2);
			finish();
			break;
			
		default:
			break;
		}
	}
}
